// 监听请求,二次封装请求,XML和fetch
export function trackRequest() {
    const needTracks = this.needTrack('requestTime')
    if (!needTracks.length) { return }
    const global = this
    const requestObj = Symbol('requestObj')
    const XMLHttpRequest = window.XMLHttpRequest
    const oldOpen = XMLHttpRequest.prototype.open
    // 开启XHL请求,直接修改它的原型,原型上没有method属性,数据将参数赋值到原型上
    XMLHttpRequest.prototype.open = function () {
        this[requestObj] = { method: arguments[0], url: arguments[1] }
        // 触发原生事件,arguments是请求参数数组,['POST','/api/cfk/payCenter/permission/getManagerCompanyIdList',true}
        oldOpen.apply(this, arguments)
    }
    // 发送XHL请求
    const oldSend = XMLHttpRequest.prototype.send
    XMLHttpRequest.prototype.send = function (body) {
        const requestBeginTime = new Date().getTime()
        const content = JSON.stringify({ body, ...this[requestObj] })
        const requestCallback = () => {
            const elseObj = { status: this.status, body, ...this[requestObj] }
            loadQuest(requestBeginTime, elseObj, content)
            this.removeEventListener('load', requestCallback)
            this.removeEventListener('error', requestCallback)
        }
        this.addEventListener('load', requestCallback)
        this.addEventListener('error', requestCallback)
        oldSend.apply(this, arguments)
    }
    // 发送fetch请求,Promise二次封装
    const oldFetch = window.fetch
    window.fetch = (url, options) => {
        const requestBeginTime = new Date().getTime()
        const elseObj = { method: !options || !options.method ? 'GET' : 'POST' }
        const content = JSON.stringify({ url, ...options })
        const fetchRequestCallback = (resObj) => {
            Object.assign(elseObj, { status: resObj.status, url })
            loadQuest(requestBeginTime, elseObj, content)
        }
        return new Promise((resolve, reject) => {
            oldFetch(url, options).then((res) => {
                resolve(res)
                fetchRequestCallback(res)
            }).catch((err) => {
                reject(err)
                fetchRequestCallback(err)
            })
        })
    }
    // 查看有没符合条件的对象
    function getConformTrack(elseObj) {
        // 查请求地址
        const conformTracks = needTracks.filter(itemA => {
            const urlArr = itemA.rule.urls.filter(item => elseObj.url === item)  // 请求地址必须完全一致,不然会有误判
            return urlArr.length
        })
        return conformTracks
    }
    // 请求成功完成的逻辑
    function loadQuest(requestBeginTime, elseObj, content) {
        const conformTracks = getConformTrack(elseObj)
        if (conformTracks.length) {
            const conformTracksItem = conformTracks[0]
            const insideObj = global.isInExecutes(conformTracksItem, content)
            insideObj.lastTime ? clearTimeout(insideObj.lastTime) : ''
            insideObj.lastTime = setTimeout(() => {
                const requestEndTime = new Date().getTime()  // 请求完成时间
                const obj = { describe: requestEndTime - requestBeginTime, ...elseObj }
                global.deleteExecutesItem(conformTracksItem, content)  // 清除记录对象
                // 上报数据
                global.sendDataBefore(conformTracks, 'requestTime', obj)
            }, 200)
        }
    }
}